﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.NetworkService.Behaviors.Interfaces;


namespace Domain.NetworkService
{
    public class UdpTestBehavior : INetworkServiceTest
    {
        public UdpPorts port { get; private set; }
        // Check if remote UDP port is open
        // BEWARE that several firewalls/network setups/IDS etc. might influence the result... the following method is NOT 100% reliable but it is the only method possible with UDP IMHO (since UDP is connectionless).
        // You can use UdpClient, set a receive timeout on the underlying socket, make a connection to that remote server/port, Send some small message (byte[] !) and call Receive.
        // IF the port is closed you get an exception saying that the connection was forcibly closed (SocketException with ErrorCode 10054 = WSAECONNRESET)... which means the port is NOT open.
        // Otherwise you either receive an answer OR a timeout exception - both should be interpreted as "the UDP port is open".

        // From nmap documentation:
        // UDP scan works by sending a UDP packet to every targeted port. For some common ports such as 53 and 161, 
        // a protocol-specific payload is sent, but for most ports the packet is empty. The --data-length option can 
        // be used to send a fixed-length random payload to every port or (if you specify a value of 0) to disable payloads. 
        // If an ICMP port unreachable error (type 3, code 3) is returned, the port is closed. Other ICMP unreachable errors 
        // (type 3, codes 1, 2, 9, 10, or 13) mark the port as filtered. Occasionally, a service will respond with a UDP packet, 
        // proving that it is open. If no response is received after retransmissions, the port is classified as open|filtered. 
        // This means that the port could be open, or perhaps packet filters are blocking the communication. 
        // Version detection (-sV) can be used to help differentiate the truly open ports from the filtered ones.
        // http://nmap.org/book/man-port-scanning-techniques.html


        public NetworkTestResult Start()
        {
            throw new NotImplementedException();
        }

        public IDescription Description { get; set; }
        public TimeSpan Timeout { get; set; }
    }
}
